package com.zzyy.study.controller;

import com.zzyy.study.service.ChatAssistant;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.output.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.List;

/**
 * @auther zzyy
 * @create 2025-03-07 20:24
 */
@RestController
@Slf4j
public class ChatApiController
{
    @Resource
    private ChatLanguageModel chatLanguageModel;
    @Resource
    private ChatAssistant chatAssistant;

    // http://localhost:9002/chatapi/lowapi?prompt=%E8%8C%85%E5%8F%B0%E9%85%92%E5%A5%BD%E5%96%9D%E5%90%97
    @GetMapping(value = "/chatapi/lowapi")
    public String lowApi(@RequestParam("prompt") String prompt)
    {
        Response<AiMessage> aiMessageResponse = chatLanguageModel.generate(UserMessage.from(prompt));

        return aiMessageResponse.content().text();
    }


    // http://localhost:9002/chatapi/lowapi2?prompt=%E8%8C%85%E5%8F%B0%E9%85%92%E5%A5%BD%E5%96%9D%E5%90%97
    @GetMapping(value = "/chatapi/lowapi2")
    public String lowApi2(@RequestParam("prompt") String prompt)
    {
        List<ChatMessage> messages = List.of(
                SystemMessage.systemMessage("假如你是一个医生，只回答医学问题,其它不回答"),
                UserMessage.userMessage(prompt)
        );
        Response<AiMessage> aiMessageResponse = chatLanguageModel.generate(messages);

        return aiMessageResponse.content().text();
    }


    @GetMapping(value = "/chatapi/highapi")
    public String highApi(@RequestParam("prompt") String prompt)
    {
        return chatAssistant.chat(prompt);
    }


    @GetMapping(value = "/chatapi/highapi2")
    public String highApi2(@RequestParam("prompt") String prompt)
    {
        return chatAssistant.chat2(prompt);
    }
}
